package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.LpsWlMapper;
import com.youlai.boot.system.service.LpsWlService;
import com.youlai.boot.system.model.entity.LpsWl;
import com.youlai.boot.system.model.form.LpsWlForm;
import com.youlai.boot.system.model.query.LpsWlQuery;
import com.youlai.boot.system.model.vo.LpsWlVO;
import com.youlai.boot.system.converter.LpsWlConverter;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 物料服务实现类
 *
 * @author lwb
 * @since 2025-07-16 21:52
 */
@Service
@RequiredArgsConstructor
public class LpsWlServiceImpl extends ServiceImpl<LpsWlMapper, LpsWl> implements LpsWlService {

    private final LpsWlConverter lpsWlConverter;

    /**
    * 获取物料分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<LpsWlVO>} 物料分页列表
    */
    @Override
    public IPage<LpsWlVO> getLpsWlPage(LpsWlQuery queryParams) {
        Page<LpsWlVO> pageVO = this.baseMapper.getLpsWlPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取物料表单数据
     *
     * @param id 物料ID
     * @return
     */
    @Override
    public LpsWlForm getLpsWlFormData(Long id) {
        LpsWl entity = this.getById(id);
        return lpsWlConverter.toForm(entity);
    }
    
    /**
     * 新增物料
     *
     * @param formData 物料表单对象
     * @return
     */
    @Override
    public boolean saveLpsWl(LpsWlForm formData) {
        LpsWl entity = lpsWlConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新物料
     *
     * @param id   物料ID
     * @param formData 物料表单对象
     * @return
     */
    @Override
    public boolean updateLpsWl(Long id,LpsWlForm formData) {
        LpsWl entity = lpsWlConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除物料
     *
     * @param ids 物料ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLpsWls(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的物料数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<LpsWl> getByNoList(Collection<String> noList) {
        return baseMapper.getByNoList(noList);
    }
}
